﻿KISSY.add("fgt/meishiSelectBox", function (S) {
    function meishiSelectBox(option) {
        var self = this;
        self._init(option);
    }
    S.augment(meishiSelectBox, {
        _init: function (option) {
            var self = this,
                $ = S.all;

            self.containerId = option.containerId;
            self.container = $(self.containerId);
            self.Selection = {};
            self.maxChecked = 2;

        },

        _bindMarkerEvents: function () {
            var self = this;
        },

        Show: function () {
            var self = this;
            KISSY.use("node, overlay, ua", function (S, Node, O, ua) {
                if (S.isUndefined(this.meishiSelectBoxDialog)) {
                    this.meishiSelectBoxDialog = new O.Dialog({
                        prefixCls: "fgt-",
                        srcNode: self.containerId,
                        elStyle: {
                            position: ua.ie == 6 ? "absolute" : "fixed"
                        },
                        // 与button对齐
                        align: {
                            points: ['cc', 'cc']
                        },

                        width: 800,
                        height: 500,
                        closable: true,
                        mask: true
                    });
                }
                this.meishiSelectBoxDialog.show();
                var ckbs = $(".level2-list-item input");

                ckbs.on("change", function (e) {
                    var t = $(this);
                    var typeId = t.attr("typeid");
                    var level = t.attr("level");
                    var isChecked = t.attr("checked") === "checked";

                    //先检查是否超出限制
                    if (isChecked === true) {
                        var selection = GetSelectionByType(typeId);
                        var length = 0;
                        KISSY.each(selection, function (v, k) {
                            length++;
                        });
                        if (length > self.maxChecked) {
                            t.attr("checked", null);
                            var groupName = $("#meishi-type-" + typeId).children("label").text();
                            alert(groupName + "：此分组最多可以选择" + self.maxChecked + "个分类");
                            return;
                        }
                    }
                    if (level == "2") {
                        var leve3div = t.siblings("div.level3-list");
                        $("div.level3-list").removeClass("showLeve3").css("display", "none"); //隐藏所有level3 元素

                        //如果leve3div存在，说明点击的是二级分类
                        if (leve3div.length > 0) {
                            leve3div.addClass("showLeve3").css("display", "inline"); //显示当前所选元素的下级分类
                            if (isChecked == false) { //如果取消选中，那么取消选中的所有子分类
                                leve3div.children("ul").children("li").children("input").attr("checked", null);
                            }
                        }
                    }
                    else if (isChecked === true) {
                        //选择第三级,检查父节点
                        t.parent().parent().parent().parent().children("input").attr("checked", "checked");
                    }
                    UpdateInfo(typeId);
                });

                function UpdateInfo(typeId) {
                    self.Selection[typeId] = GetSelectionByType(typeId);
                    var length = 0,
                        message = "";

                    KISSY.each(self.Selection[typeId], function (e) {
                        length++;
                        if (message.length != 0) message += "，";
                        message += e.name;
                    });
                    if (message.length > 0) {
                        message = "您已经选了" + length + "个分类 : " + message;
                    }
                    $("#meishi-info-" + typeId).text(message);
                };

                function GetSelectionByType(typeId) {
                    var result = {},
                    root = $("#meishi-type-" + typeId);
                    var level2List = root.children("ul").children("li.level2-list-item").children("input")
                    KISSY.each(level2List, function (e) {
                        var level2 = $(e);
                        if (level2.attr("checked") === "checked") {
                            var level3List = level2.siblings("div.level3-list");
                            var hasChildrenChecked = false;
                            KISSY.each(level3List.children("ul").children("li").children("input"), function (e) {
                                var level3 = $(e);
                                if (level3.attr("checked") === "checked") {
                                    hasChildrenChecked = true;
                                    result[level3.val()] = { id: level3.attr["id"], name: level2.siblings("label").text() + "->" + level3.siblings("label").text() };
                                }
                            });
                            if (hasChildrenChecked === false) {
                                result[level2.val()] = { id: level2.attr["id"], name: level2.siblings("label").text() };
                            }
                        }
                    });
                    return result;
                }
            });
        },

        GetSelection: function () {
            return self.Selection;
        }

    });

    return meishiSelectBox;
});